{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "face= 1\n",
      "eyes= 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "三个问题\n",
    "1.特征：像素经过运算(一定大小的滤波器遍历图片进行卷积运算)后得到的结果(具体值 向量 矩阵)\n",
    "2.判决方法：通过阈值判决(小于或大于某个值则为目标)\n",
    "3.如何得到判决方法：通过机器学习获得指标(阈值)\n",
    "\n",
    "haar特征计算方式\n",
    "1.白色-黑色\n",
    "2.整个区域*权重+黑色*权重\n",
    "3.（p1-p2-p3+p4）*w\n",
    "\n",
    "Adaboost分类器\n",
    "通过级联分类器对特征进行判断：haar > T1 and haar > T2\n",
    "流程：强分类器级联-》弱分类器级联-》特征Node\n",
    "训练终止条件：1.自定义次数 2.控制误差范围\n",
    "强分类器的作用：判决\n",
    "弱分类器的作用：计算强分类器特征\n",
    "Node的作用：计算弱分类器的特征\n",
    "每一个弱分类器至多对应三个特征，每个特征对应一个node结点\n",
    "\n",
    "Adaboost训练\n",
    "1.初始化权值分布（所有权值要想等）\n",
    "2.遍历判决阈值计算误差概率\n",
    "3.选取最小的误差概率和其对应的权值，计算权重系数\n",
    "4.更新权值分布\n",
    "'''\n",
    "import cv2\n",
    "import numpy as np\n",
    "# 步骤 1.load xml文件 2.加载图片 3.灰度处理计算haar特征 4.检测 5.遍历检测结果并用方框标注\n",
    "# load xml文件\n",
    "face_xml = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')\n",
    "eye_xml = cv2.CascadeClassifier('haarcascade_eye.xml')\n",
    "# 加载图片\n",
    "img = cv2.imread('face.jpg')\n",
    "cv2.imshow('img',img)\n",
    "# 灰度处理计算haar特征,在计算haar特征之前要进行灰度处理\n",
    "gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n",
    "# 检测 参数1：索要检测的灰度图 参数2：缩放系数 参数3：目标大小单位为像素\n",
    "faces = face_xml.detectMultiScale(gray,1.3,5)\n",
    "print('face=',len(faces))\n",
    "# 遍历检测结果并用方框标注\n",
    "for (x,y,w,h) in faces:\n",
    "    # 绘制方框\n",
    "    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)\n",
    "    roi_face = gray[y:y+h,x:x+w]\n",
    "    roi_colorFace = img[y:y+h,x:x+w]\n",
    "    eyes = eye_xml.detectMultiScale(roi_face)\n",
    "    print('eyes=',len(eyes))\n",
    "    for (eX,eY,eW,eH) in eyes:\n",
    "        cv2.rectangle(img,(x+eX,y+eY),(x+eX+eW,y+eY+eH),(0,255,0),2)\n",
    "cv2.imshow('imgDetect',img)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
